/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Graphics2D;
import net.worlds.Main;
import net.worlds.input.InputState;
import net.worlds.ui.UI;


public class EMain extends EFrame{
    private EComponent dragComponent = null;
    protected EComponent mouseComponent = null;
    public EMain(int width, int height)
    {
        super(0, 0, width, height);
        isPrimary = true;
        EDGESettings.main = this;
    }
    @Override
    public void render(Graphics2D g)
    {
        super.render(g);
        g.setFont(EDGESettings.defaultFont);
        EComponent mc;
        try
        {
        if(mouseComponent != null && mouseComponent.tooltip != null)
        {
            int edgeX = width;
            mc = mouseComponent;
            int tx = InputState.mouseX + 20;
            if(tx + mc.tooltipWidth + 4 > edgeX)
                tx = edgeX - mc.tooltipWidth - 8;
            int ty = InputState.mouseY + 32;
            if(ty < 60)
                ty = 60;
            g.setColor(tooltipColor);
            g.fillRect(tx - 2, ty - 2, mc.tooltipWidth + 4, mc.tooltipHeight + 4);
            g.setColor(tooltipBorderColor);
            g.drawRect(tx - 3, ty - 3, mc.tooltipWidth + 5, mc.tooltipHeight + 5);
            g.setFont(EDGESettings.defaultFont);
            g.drawString(mc.tooltip, tx, ty + mc.tooltipHeight);
        }
        }catch(NullPointerException ex){}
    }
    public void setMouseComponent(EComponent c)
    {
        EComponent oldMouseComponent = mouseComponent;
        if(oldMouseComponent == c)
            return;
        if(oldMouseComponent != null)
        {
            oldMouseComponent.mouseOff();
            oldMouseComponent.mouseOver = false;
        }
        mouseComponent = c;
        if(c != null)
        {
            mouseComponent.mouseOver();
            if(mouseComponent != null)
                mouseComponent.mouseOver = true;
        }
    }
    public void setSize(int w, int h)
    {
        width = w;
        height = h;
        if(this instanceof EditorTop)
            ((EditorTop)this).setSizes();
    }
    public boolean eMouseDown(int b, int x, int y)
    {
        if(dragComponent == null)
        {
            EComponent c = getChildAt(x, y);
            if(c != null)
            {
                boolean r = c.mouseDown(b, x, y);
                if(!c.fixed && c instanceof EFrame)
                {
                    //c.putOnTop();
                    //dragComponent = c;
                }
                if(!c.clickThrough && !r)
                    return true;
            }
        }
        if(this instanceof EditorTop)
            for(EComponent c : EDGESettings.menuBar.getChildren(1))
            {
                if(c instanceof EMenuTitle && c.visible && c != mouseComponent)
                {
                    ((EMenuTitle)c).hideMenu();
                }
            }
        return false;
    }
    public boolean eMouseUp(int b, int x, int y)
    {
        EComponent c = getChildAt(x, y);
        if(c != null)
        {
            c.mouseUp(b, x, y);
            return true;
        }
        return false;
    }
    public boolean eMouseMove(int x, int y)
    {
        EComponent c = getChildAt(x, y);
        if(c != null)
        {
            if(c.mouseMove(x, y, InputState.pmouseX, InputState.pmouseY))
                EDGESettings.main.setMouseComponent(c);
            return true;
        }
        else
        {
            EDGESettings.main.setMouseComponent(null);
            if(UI.mouseOverUI)
            {
                UI.mouseOverUI = false;
                Main.mouseOffUI();
                UI.mouseOffUI();
            }
        }
        return false;
    }
    public boolean eMouseDrag(int b, int x, int y)
    {
        if(dragComponent != null)
        {
            boolean r = dragComponent.mouseDrag(x, y, InputState.pmouseX, InputState.pmouseY);
            if(!dragComponent.clickThrough && r)
            return true;
        }
        return false;
    }
}
